package io.renren.modules.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.customer.dao.MessageReplyDao;
import io.renren.modules.customer.entity.MessageReplyEntity;
import io.renren.modules.customer.service.MessageReplyService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("messageReplyService")
public class MessageReplyServiceImpl extends ServiceImpl<MessageReplyDao, MessageReplyEntity> implements MessageReplyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageReplyServiceImpl.class);

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<MessageReplyEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank((String) params.get("mobilePhone"))) {
            queryWrapper.eq(MessageReplyEntity::getMobilePhone, ((String) params.get("mobilePhone")).trim());
        }
        if (StringUtils.isNotBlank((String) params.get("batchNumber"))) {
            queryWrapper.eq(MessageReplyEntity::getBatchNumber, ((String) params.get("batchNumber")).trim());
        }
        if (StringUtils.isNotBlank((String) params.get("serialNumber"))) {
            queryWrapper.eq(MessageReplyEntity::getSerialNumber, ((String) params.get("serialNumber")).trim());
        }
        if (StringUtils.isNotBlank((String) params.get("messageId"))) {
            queryWrapper.eq(MessageReplyEntity::getMessageId, ((String) params.get("messageId")).trim());
        }
        if (StringUtils.isNotBlank((String) params.get("replyContent"))) {
            queryWrapper.isNotNull(MessageReplyEntity::getReplyContent);
            queryWrapper.like(MessageReplyEntity::getReplyContent, "%" + ((String) params.get("replyContent")).trim() + "%");
        }
        IPage<MessageReplyEntity> page = this.page(
                new Query<MessageReplyEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public MessageReplyEntity getByVendorMessageIdAndPhone(String messageId, String mobilePhone) {
        if (StringUtils.isBlank(messageId)) {
            return null;
        } else {
            LambdaQueryWrapper<MessageReplyEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MessageReplyEntity::getMessageId, messageId.trim());
            if (StringUtils.isNotBlank(mobilePhone)) {
                queryWrapper.eq(MessageReplyEntity::getMobilePhone, mobilePhone.trim());
            }
            List<MessageReplyEntity> entities = this.baseMapper.selectList(queryWrapper);
            if (entities != null && entities.size() > 0) {
                return entities.get(0);
            } else {
                return null;
            }
        }
    }

    @Override
    public List<MessageReplyEntity> getRepliesByIds(List<Long> ids) {
        if (ids == null || ids.size() < 1) {
            return Collections.emptyList();
        } else {
            LambdaQueryWrapper<MessageReplyEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(MessageReplyEntity::getId, ids);
            return this.baseMapper.selectList(queryWrapper);
        }
    }
}